/*

[{"itemDesc":"左眼","itemName":"OS","kind":1,"pid":5,"itemId":"7"},{"itemDesc":"戴镜","itemName":"戴镜","kind":0,"pid":0,"itemId":"5"},{"itemDesc":"裸眼","itemName":"裸眼","kind":0,"pid":0,"itemId":"1"},{"itemDesc":"双眼","itemName":"OU","kind":1,"pid":5,"itemId":"8"},{"itemDesc":"右眼","itemName":"OD","kind":1,"pid":5,"itemId":"6"},{"itemDesc":"右眼","itemName":"OD","kind":1,"pid":1,"itemId":"2"},{"itemDesc":"左眼","itemName":"OS","kind":1,"pid":1,"itemId":"3"},{"itemDesc":"双眼","itemName":"OU","kind":1,"pid":1,"itemId":"4"}]

*/

function getItemTree(d) {
  let temp = {};
  for (let k in d) {
    if (!temp[d[k].id + "_" + d[k].itemId]) {
      temp[d[k].id + "_" + d[k].itemId] = d[k];
    }
  }
  let tree = [];
  for (let i in temp) {
    if (temp[i].pid) {
      if (!temp[temp[i].id + "_" + temp[i].pid].children) {
        temp[temp[i].id + "_" + temp[i].pid].children = [];
      }
      temp[i]["level"]++;
      temp[temp[i].id + "_" + temp[i].pid].children.push(temp[i]);
    } else {
      tree.push(temp[i]);
    }
  }
  return tree;
}

export function getTree(d) {
  //先整合相同项目
  let project = {};
  for (let k in d) {
    //病历或检查项信息项
    let p = d[k];
    if (!project[p.id]) {
      //不存在相同的project
      project[d[k].id] = {
        seq: p.seq,
        version: p.version,
        id: p.id,
        parentId: p.parentId,
        projectName: p.projectName,
        project: p.project,
        projectDesc: p.projectDesc,
        clinicId: p.clinicId,
        level: 0,
      };
      project[d[k].id].children = [];
    }
    //指标项信息注入children列表,根据病历或检查项id归类
    if (p.itemName) {
      project[p.id].children.push({
        id: p.id,
        itemId: p.itemId,
        pid: p.pid,
        itemName: p.itemName,
        itemDesc: p.itemDesc,
        isReported: p.isReported,
        defaulted: p.defaulted,
        kind: p.kind,
        readOnly: p.readOnly,
        available: p.available,
        dictId: p.dictId,
        level: 0,
      });
    }
  }
  let tree = [];
  for (let i in project) {
    if (project[i].children.length > 0) {
      project[i].children = this.getItemTree(project[i].children);
    }
    if (project[i].parentId && project[project[i].parentId]) {
      if (!project[project[i].parentId].children) {
        project[project[i].parentId].children = [];
      }
      project[i]["level"]++;
      project[project[i].parentId].children.push(project[i]);
    } else {
      tree.push(project[i]);
    }
  }
  return tree.sort(function (a, b) {
    return a.seq - b.seq;
  });
}

/*let d = [{"clinicId":1,"id":1,"isReported":"1","parentId":0,"project":"基础检查"},
{"clinicId":1,"id":2,"isReported":"1","parentId":0,"project":"屈光检查"},
{"clinicId":1,"id":10,"isReported":"1","parentId":1,"project":"主视眼"},
{"clinicId":1,"id":8,"isReported":"1","parentId":1,"project":"原镜度数"},
{"clinicId":1,"id":9,"isReported":"1","parentId":1,"project":"近附加"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"左眼","itemId":"7","itemName":"OS","kind":1,"parentId":1,"pid":5,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"戴镜","itemId":"5","itemName":"戴镜","kind":0,"parentId":1,"pid":0,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"裸眼","itemId":"1","itemName":"裸眼","kind":0,"parentId":1,"pid":0,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"双眼","itemId":"8","itemName":"OU","kind":1,"parentId":1,"pid":5,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"右眼","itemId":"6","itemName":"OD","kind":1,"parentId":1,"pid":5,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"右眼","itemId":"2","itemName":"OD","kind":1,"parentId":1,"pid":1,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"左眼","itemId":"3","itemName":"OS","kind":1,"parentId":1,"pid":1,"project":"远视力"},
{"clinicId":1,"id":3,"isReported":"1","itemDesc":"双眼","itemId":"4","itemName":"OU","kind":1,"parentId":1,"pid":1,"project":"远视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"戴镜","itemId":"5","itemName":"戴镜","kind":0,"parentId":1,"pid":0,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"双眼","itemId":"8","itemName":"OU","kind":1,"parentId":1,"pid":5,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"裸眼","itemId":"1","itemName":"裸眼","kind":0,"parentId":1,"pid":0,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"双眼","itemId":"4","itemName":"OU","kind":1,"parentId":1,"pid":1,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"右眼","itemId":"6","itemName":"OD","kind":1,"parentId":1,"pid":5,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"右眼","itemId":"2","itemName":"OD","kind":1,"parentId":1,"pid":1,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"左眼","itemId":"7","itemName":"OS","kind":1,"parentId":1,"pid":5,"project":"近视力"},
{"clinicId":1,"id":4,"isReported":"1","itemDesc":"左眼","itemId":"3","itemName":"OS","kind":1,"parentId":1,"pid":1,"project":"近视力"}];
*/
//getTree(d)
